package nuc.sw.action;

import java.util.List;

import javax.annotation.Resource;

import nuc.sw.entity.User;
import nuc.sw.service.UserService;

import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Component;

import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ModelDriven;

@Component("userAction")
@Scope("prototype")
public class UserAction implements ModelDriven<User>{
	private User user ;
	private UserService userService;
	
	public User getUser() {
		return user;
	}

	@Resource
	public void setUser(User user) {
		this.user = user;
	}

	public UserService getUserService() {
		return userService;
	}
	
	@Resource(name="userService")
	public void setUserService(UserService userService) {
		this.userService = userService;
	}
	
	public String addUserMethod() throws Exception{
		userService.addUser(user);
		return "success";
	}
	
	public String deleteUserMethod() throws Exception{
		userService.deleteUser(user);
		return "success";
	}
	
	public String updateUserMethod() throws Exception{
		userService.updateUser(user);
		return "success";
	}
	
	public String listUsers() throws Exception{
		List<User> users = userService.findAllUser();
		ActionContext.getContext().put("userlist", users);
		return "listOK!";
	}

	public User getModel() {
		return user;
	}
}
